package com.imy.virtual.helper;

import java.util.ArrayList;
import java.util.List;

import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;

public class ModeDexLoader {
    private List<DexClassLoader> mClassLoaderList = null;

    public ModeDexLoader() {
        mClassLoaderList = new ArrayList<DexClassLoader>();
    }

    /**
     * 添加一个插件到当前的classLoader中
     */
    protected void addModeClassLoader(DexClassLoader dexClassLoader) {
        mClassLoaderList.add(dexClassLoader);
    }

    public Class<?> loadClass(String className) {
        //挨个的到插件里进行查找
        Class<?> clazz = null;
        if (mClassLoaderList != null) {
            for (DexClassLoader classLoader : mClassLoaderList) {
                if (classLoader == null) continue;
                try {
                    clazz = classLoader.loadClass(className);
                    if (clazz!=null){
                        return clazz;
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
